# DERIVED from tconfig.awk. Analyzis tiling/config files.
# CONCEPT for testing tiling/config/flip-replay input file
# Lets denote line X COL Y in the file as [X,Y].
# nodes=[3,1]
# multi=[3,2]
# deconame=[3,3] {only config+flip-replay}
# freplay=[3,4] {only flip-replay}
# 1. When (FNR=nodes+3), this must be TILING only.
# 2. When (FNR>nodes+3) and freplay="flip_replay", it's flip-replay type
# 3. When (FNR>nodes+3) and not(freplay), it's CONFIG
# Now, consistency checks.
# TILING:
#   (T1) First two lines must have 5 cols (5D basis vectors)
#   (T2) multi must have positive and not very big value
#   (T3) each (line>3) must have 5 or more columns (5D int vector, perhaps+smth)
# CONFIG: 
#   (C1) must satisfy T1-T3
#   (C2) must have subsequently valid parameter part
#   (C3) must have subsequently valid atom-def part
#   OBSOLETE: (C4) must have subsequently valid objects-def part
#   OBSOLETE: (C5) must have subsequently orbit-status part
# FLIP-REPLAY:
#   (F1) T1-T3
#   The subsequent flip-info part has 4 cols: VERTEX U dU accept_status
#   (F2) VERTEX must be in the range [1-nodes]
#   (F2) U[n+1]=U[n]+dU[n+1]
#   (F4) accept status is in the range [0-1]

BEGIN {  # options
  for (i = 1; i < ARGC; i++) {
    if (ARGV[i] == "-s"){
      silent = 1;
      delete ARGV[i];
    }
    if (ARGV[i] == "-p"){
      showpara = 1;
      delete ARGV[i];
    }
    if (ARGV[i] == "-occ"){
      occ = 1;
      delete ARGV[i];
    }
    if (ARGV[i] == "-occ5"){
      occ = 1; repre5=1;
      delete ARGV[i];
    }
  }
}
FNR==1{cl1=NF}
FNR==2{cl2=NF}
FNR==3{nn=$1;mu=$2;deco=$3;tend=nn+4}
FNR==3&&$4=="flip_record"{frep=1; uer=0; as=0; flpe=0}
FNR>3&&FNR<=nn+3{
#  if(NF<5){nc5++};
  for(i=1;i<=5;i++){if($i~/[[:alpha:]]/){nc5++}};
  nu=0;for(i=1;i<=5;i++){nu+=$i};
#  nu=nu%5;if(nu>2){nu-=5};if(nu<-2){nu+=5};
  nnu[nu]++;
#  print FNR, nc5," -> ",$0
}
frep==0&&FNR==tend{pp=$1} # START PARAMETER SECTION
frep==0&&FNR>tend&&tend>0&&FNR<=(tend+pp)&&popa+1==$1{ #parameter section
    popa++; pars[popa]=$0; op[popa]=$2;
  if(popa==pp){poobjs=FNR+1}
}
frep==0&&FNR==poobjs{poa=FNR+$1+1} # object origins
readatoms==1&&FNR<poa{
  sites++;
  ochn[$10]++;
  if($9>0){nat++;}
  if(maxorb<$10){maxorb=$10};
  ch[$9]++; 
  if(!occ){
    oche[$10,$9]++;
    if($9>0){ochnat[$10]++};
  }else{
    c=11; if(repre5){c=16};
#    print "OCC",c,$c;
    for(i=1;i<=$c;i++){
      j=2*(i-1)+c+1;
#      if($(j+1)>0.0001){
      ochnat[$10]+=$(j+1);
      oche[$10,$j]+=$(j+1);
      toc+=$(j+1)}; # total occupancy; check against nat
#    }
  }
}
frep==0&&$3=="atoms"&&FNR==poa{ # ATOM SECTION
    readatoms=1;
    nna++;
    poa+=$1+1;
    if(nna==nn){poo=poa}
}
#CANCEL OBJECT SECTION
#frep==0&&$3=="objects"&&FNR==poo{ # OBJECTS SECTION
#  nno++;
#  poo+=$1*4+1;
#  if(nno==nn){pof=poo;poo=0}
#}
#frep==0&&$2~"orbits"&&FNR==pof{norb=$1} #beg orbit-status section
#frep==0&&FNR>pof&&pof>0&&corb+1==$1{corb++} #orbit-status
frep==1&&FNR>=tend&&$2=="couple"{frep=2;couple=$1;if(couple<0||couple>1){ecoupl=2}} # line 1 after tiling is COUPLE
frep==1&&FNR>=tend&&$2!="couple"{ecoupl=1}
frep==2&&FNR>=tend+1{
  flp++; if($4==0){flprej++}; if($4==1){flpac++};
  if(flp>1){dd=$2-$3-uo;if((dd)^2>1e-5){uer++}};
  if(($4>1)||($4<0)){as++};
  if($1>nn||$1<1){flpe++};
  if($4==1)uo=$2
}
END{
# determine file type
  if (!bgerr){
    er=0;
    tinp=1;
    if(FNR==tend-1||nna==0){tinp=0};
    if((FNR>tend-1)&&(frep==2)){tinp=2};
# errors
    if((tinp==1)&&(nna!=nn)){er=9;ems="Error in atom-def section."};
    if((tinp==1)&&(popa!=pp)){er=8;ems="Error in parameter section."};
# {flip-record: 5-7}
    if(uer>0){er=7;ems="Inconsistent flip-record energies."};
    if(as>0){er=6;ems="col4 (accept-status) can be only 0 or 1."};
    if(flpe>0){er=5;ems="Vertex number points to non-existent vertex (col1 after tiling section)."};
    if(ecoupl==1){er=5; ems="Probably obsolete flip.rec: 1st line after tiling should be integer COUPLE"}
    if(ecoupl==2){er=5; ems="Integer couple has to be 0 or 1."}
# {tiling: 1-3}
    if(tend-1>FNR){er=4;ems="Too few tiling vertices."}
    if(nc5>0){er=3;ems="Error in the tiling-body: some vertex has improper 5D definition."};
#  if(mu<1||mu>1000){er=2;ems="Stacking multiplicity (line3 col2) is ill-defined."};
    if(mu~/[[:alpha:]]/||mu<1){er=2;ems="Stacking multiplicity (line3 col2) is ill-defined."};
    if ((cl1<5)||(cl2<5)){er=1;ems="First two lines are not 5D basis."};
  }else{er=1}
  if(showpara==1){
    if(pp!=""){
      print pp, "parameters";
      for(i=1;i<=pp;i++){if(ochn[op[i]]>0){print pars[i]}}
    }else{print "File does not contain parameters section."}
  }
  if(!silent){ #this is info!
    if(er>0){print ems}else if(showpara==0){
      print ARGV[1]":", nn,"nodes,",mu,"stacked layer(s).";
      print "Tiling discrete-perp-level statistics: ";
      for(item in nnu){
	printf("%2i %4i\n",item,nnu[item]);
      };
      if(tinp==1){
	print "Decoration: ",deco ;
	print nat, "atoms,", sites, "sites,", (sites-nat), "vacant" ;
	for(i=1; i<=110; i++){if(ch[i]>0){printf "  %2i %6i %8.3f\n", i, ch[i], ch[i]/nat}};
	print maxorb, "orbits: ORB# WEIGHT OCC CHEM CHOCC... (for nat>0 && Z>0 only):";
	for(i=1;i<=maxorb;i++){if(ochnat[i]>0.0005){printf "  %3i %6.3f %6.3f ",i,ochnat[i]/nat,ochnat[i]/ochn[i] ;
	for(j=1;j<=110;j++){if(oche[i,j]>0.0005){printf "  %2i %6.3f ",j,oche[i,j]/ochnat[i]}};
	printf"\n"
	  }
	}
      }
      if(tinp==2){
	print "Flip-record file, couple="couple", "flpac" accepted, "flprej" rejected flips.";
      }
    }
  }else{print tinp, er, deco, ems} #this is for d03
}
